package com.axios.controller.servlets;

import com.axios.service.JsonServerService;
import com.axios.utils.JsonBale;
import org.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/showJsonServer")
public class JsonServerServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应头允许跨域
        response.setHeader("Access-Control-Allow-Origin","*");

        // 响应头
        response.setHeader("Access-Control-Allow-Headers","*");

        // 打包json类
        JsonBale jsonBale = new JsonBale();

        // 把对象数组变成json方式
        JSONArray array = jsonBale.creatJSONArr(JsonServerService.link());

        // 设置响应信息
        response.getWriter().write(array.toString());
    }
}
